#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include "can.h"
#include "../udp/in_temp.h"
#include "../pcm/pcm.h"

#define DEBUG    0

#define ERR_MSG(fmt, args...)    fprintf(stderr, fmt, ##args)

#if DEBUG
    #define DBG_MSG(fmt, args...)    fprintf(stdout, fmt, ##args)
#else
    #define DBG_MSG(fmt, args...)
#endif

#ifndef PF_CAN
    #define PF_CAN 29
#endif

#ifndef AF_CAN
    #define AF_CAN PF_CAN
#endif



/*************************
 *
 *
 *************************/
/* CAN鎬荤嚎鍙橀噺鍙婄紦鍐插尯澹版槑瀹氫箟  */
int flag_orbit, indirect_end, gps_end, bds_end, acs_end, orbit_end, tlegps_end, tlebds_end;//CAN鎸囦护鎺ユ敹瀹屾垚鏍囧織鍙橀噺锛岄�氳繃UDP閫忎紶
int gps_length, bds_length, acs_length, orbit_length, tlegps_length, tlebds_length, gps_recvdlc, bds_recvdlc, acs_recvdlc, orbit_recvdlc, tlegps_recvdlc, tlebds_recvdlc; //bds銆乬ps銆乤cs銆乼le鏁版嵁鍖呭寘闀�
static int count_yc=0, count_gb=0, count_jj_all, count_nw;//杞閬ユ祴銆佸箍鎾抚璁℃暟銆侀棿鎺ユ寚浠よ鏁般�佸唴鍔℃寚浠よ鏁�
int count_gps1, count_bds1, count_acs1, count_orbit1, count_tlegps1, count_tlebds1;
unsigned char tx2_state = 0;//TX2杩愯鐘舵��
int count_z7;
int fd_a, fd_b;
//浠呬娇鐢ㄥ湪CAN_2鍜孋AN_3绾跨▼
static int fd_2;
static struct sockaddr_can addr_can0, addr_can1;
static int can_init_ready = 0;


unsigned char senddata_yc[CAN_YC_MAX] = {0};//閬ユ祴杞搴旂瓟甯�24瀛楄妭鏈夋晥鏁版嵁+6瀛楄妭涓诲澶�+1瀛楄妭鍓澶�+1瀛楄妭sum
unsigned char senddata_gb[CAN_GB_MAX] = {0};//閬ユ祴骞挎挱搴旂瓟甯�62瀛楄妭鏈夋晥鏁版嵁+6瀛楄妭涓诲澶�+1瀛楄妭鍓澶�+1瀛楄妭sum
unsigned char indirect_data[CAN_INDIRECT] = {0};//CAN鎺ユ敹闂存帴鎸囦护锛�4瀛楄妭鏈夋晥鏁版嵁缂撳瓨
unsigned char gps_recvdata[CAN_GNSS_MAX] = {0};//GPS鏁版嵁鍖呮帴鏀剁紦瀛樺尯锛�6瀛楄妭涓诲澶�+1瀛楄妭鍓澶�+435瀛楄妭鏈夋晥鏁版嵁+1瀛楄妭SUM
unsigned char bds_recvdata[CAN_GNSS_MAX] = {0};//BDS鏁版嵁鍖呮帴鏀剁紦瀛樺尯锛�6瀛楄妭涓诲澶�+1瀛楄妭鍓澶�+435瀛楄妭鏈夋晥鏁版嵁+1瀛楄妭SUM
unsigned char tlegps_recvdata[CAN_TLE_MAX] = {0};//GNSS鏄熷巻鏁版嵁鍖呮帴鏀剁紦瀛樺尯锛�6瀛楄妭涓诲澶�+1瀛楄妭鍓澶�+202瀛楄妭鏈夋晥鏁版嵁+1瀛楄妭SUM
unsigned char tlebds_recvdata[CAN_TLE_MAX] = {0};//GNSS鏄熷巻鏁版嵁鍖呮帴鏀剁紦瀛樺尯锛�6瀛楄妭涓诲澶�+1瀛楄妭鍓澶�+202瀛楄妭鏈夋晥鏁版嵁+1瀛楄妭SUM
unsigned char acs_recvdata[CAN_ACS_MAX] = {0};//ACS鏄熸晱鏁版嵁鍖呮帴鏀剁紦瀛樺尯锛�6瀛楄妭涓诲澶�+1瀛楄妭鍓澶�+244瀛楄妭鏈夋晥鏁版嵁+1瀛楄妭SUM
unsigned char orbit_recvdata[CAN_ORBIT_MAX] = {0};//ACS杞ㄩ亾鏁版嵁鍖呮帴鏀剁紦瀛樺尯锛�6瀛楄妭涓诲澶�+1瀛楄妭鍓澶�+55瀛楄妭鏈夋晥鏁版嵁+1瀛楄妭SUM


extern int count_dg, count_jj, count_gps, count_bds, count_acs, count_tlegps, count_tlebds, count_rs, count_tx2yc, flag_reset, count_pcm, count_gtxsd, count_sar, count_udpsd;
extern unsigned char timeout_flag;//AI璁＄畻鏈哄伐浣滆秴鏃舵爣蹇楀彉閲�
extern unsigned char tx2_yc[];//TX2閬ユ祴鏁版嵁10瀛楄妭
extern int change_422;//RS422閫氶亾鍒囨崲鏍囧織鍙橀噺
extern int mode_flag;//AI宸ヤ綔妯″紡鏍囧織


/* 缁撴瀯浣撳彉閲忓垵濮嬪寲CAN鎬荤嚎鎺ユ敹涓嶅悓鏁版嵁鍖呭抚id鍊� */
struct canbus_ID aican_ID = {
		0x000004e2,   //杞閬ユ祴銆佸箍鎾�
		0x000005f7,   //GNSS骞挎挱
		0x000004e7,   //GNSS鏄熷巻骞挎挱
        0x00000587,   //ACS鏄熸晱骞挎挱
		0x000002e0,   //闂存帴鎸囦护
		0x000002e2,   //鍐呭姟鎸囦护
		0x00000577,   //ACS杞ㄩ亾骞挎挱
};


/* 娴嬭瘯鍙橀噺  */
int acs_txt, gnss_txt, tle_txt;//娴嬭瘯CAN鎬荤嚎A/B骞挎挱鏁版嵁鍐欏叆txt鏂囦欢
int test_id = 0, test_id_1 = 0;


/* CAN0鍙戦�佸甯у寘鏍煎紡鏁版嵁 ,data_type涓哄簲绛斿抚绫诲瀷锛堝壇瀵煎ご锛氶仴娴�0x01/骞挎挱0x02锛� */
void senddata_normal(int data_type, int send_fd, struct sockaddr *addr_yc, int addr_size)
{
	struct can_frame senddata;
	int gb_sum=0, yc_sum=0;//绮惧瘑瀹氳建骞挎挱/AI閬ユ祴鏁版嵁鍖卻um

	if( data_type == 0x01 )//閬ユ祴鍖�
	{
		senddata.can_id = 0x4e7;
		senddata.can_dlc= 0x08;

        if( count_yc == 0x3FFF )
        	count_yc = 1;
        else
        	count_yc++;
        /* 閬ユ祴鍖呬富瀵煎ご+鍓澶�*/
        senddata_yc[0] = 0x48;
        senddata_yc[1] = 0xb2;
        senddata_yc[2] = ((count_yc >> 8) | 0xc0);
        senddata_yc[3] = (count_yc & 0xff);
        senddata_yc[4] = (CAN_YC_NUM + 1) >> 8;
        senddata_yc[5] = CAN_YC_NUM + 1;
        senddata_yc[6] = 0x01;

/* Z7閬ユ祴閲�10瀛楄妭*/
		senddata_yc[7]  = measure();         //W1:Z7娓╁害
		senddata_yc[8]  = timeout_flag;      //W2:AI宸ヤ綔瓒呮椂缃綅
		senddata_yc[9]  = count_jj_all;      //W3:AI闂存帴鎸囦护鎺ユ敹鎬昏鏁�
		senddata_yc[10] = count_jj;          //W4:TX2闂存帴鎸囦护搴旂瓟璁℃暟
		senddata_yc[11] = count_dg >> 8;     //W5-W6:绮惧瘑瀹氳建tx2搴旂瓟璁℃暟
		senddata_yc[12] = count_dg;
		senddata_yc[13] = count_pcm >> 8;    //W7-W8:楂橀�熶笂琛岄�氶亾鏁版嵁鎺ユ敹璁℃暟
		senddata_yc[14] = count_pcm;
		senddata_yc[15] = count_gtxsd >> 16;  //W9-W11:鏁颁紶B閫氶亾鏁版嵁鍙戦�佽鏁�
		senddata_yc[16] = count_gtxsd >> 8;
		senddata_yc[17] = count_gtxsd;
		senddata_yc[18] = count_udpsd >> 8;  //W12-W13:TX2楂橀�熶笂琛屾暟鎹彂閫佽鏁�
		senddata_yc[19] = count_udpsd;
		senddata_yc[20] = count_rs;    	     //W14:TX2楂橀�熶笂琛屾暟鎹潡鎺ユ敹搴旂瓟璁℃暟
		senddata_yc[21] = count_sar >> 16;    //W15-W17:SAR澶勭悊鏁版嵁ZYNQ鎺ユ敹璁℃暟
		senddata_yc[22] = count_sar >> 8;
		senddata_yc[23] = count_sar;

/* TX2閬ユ祴閲�9瀛楄妭:CPU浣跨敤鐜囥�佸唴瀛樺崰鐢ㄧ巼銆佺‖鐩樹娇鐢ㄧ巼銆佹俯搴︺�佸畾杞ˋPP鏄惁寮�鍚�佸畾杞ˋPP杩愯鐘舵�併�乀X2鐘舵�佽鏁般�佷繚鐣�1瀛楄妭銆丄I鐘舵�佹爣璇嗐�乑7绱绉掓暣鏁�*/
		memcpy(&senddata_yc[24], tx2_yc, 8);//鍒濆涓�0xAA
		senddata_yc[32] = mode_flag | tx2_state | change_422;//W2:AI鐘舵�佹爣璇�
		senddata_yc[33] = count_z7;          //Z7绱绉掓暣鏁�

    	/* 鍙戦�侀仴娴嬪寘鏁版嵁*/
        for(int index=0; index <CAN_YC_INDEX; index++)
        {
			if( index == 0)
			{
				senddata.data[0] = 0x40;
				memcpy(&senddata.data[1], senddata_yc, 7);
				sendto(send_fd, &senddata, sizeof(struct can_frame), 0, addr_yc, addr_size);
			}
			else if( (index > 0) && (index < CAN_YC_INDEX - 1) )
			{
				usleep(300);
				senddata.data[0] = index;
				memcpy(&senddata.data[1], &senddata_yc[index*7], 7);
				sendto(send_fd, &senddata, sizeof(struct can_frame), 0, addr_yc, addr_size);
			}
			else if( index == CAN_YC_INDEX - 1 )
			{
				usleep(300);
				senddata.data[0] = 0x80 | index;
				if( CAN_YC_MOD == 0 )
					memcpy(&senddata.data[1], &senddata_yc[index*7], 6);
				else
				{
					senddata.can_dlc= CAN_YC_MOD+1;
					memcpy(&senddata.data[1], &senddata_yc[index*7], CAN_YC_MOD-1);
				}
				for( int i=0; i < CAN_YC_MAX-1; i++ )
				{
					yc_sum += senddata_yc[i];
				}
				senddata.data[senddata.can_dlc-1] = yc_sum;
				sendto(send_fd, &senddata, sizeof(struct can_frame), 0, addr_yc, addr_size);
			}
        }
	}
	else if( data_type == 0x02 )
	{
		senddata.can_id = 0x4cf;
		senddata.can_dlc= 0x08;

        if( count_gb == 0x3FFF )
        	count_gb = 1;
        else
        	count_gb++;

        senddata_gb[0] = 0x48;
        senddata_gb[1] = 0xb2;
        senddata_gb[2] = ((count_gb >> 8) | 0xc0);
        senddata_gb[3] = (count_gb & 0xff);
        senddata_gb[4] = (CAN_GB_NUM + 1) >> 8;
        senddata_gb[5] = CAN_GB_NUM + 1;
        senddata_gb[6] = 0x02;

        for(int index=0; index <CAN_GB_INDEX; index++ )
        {
			if( index == 0)
			{
				senddata.data[0] = 0x40;
				memcpy(&senddata.data[1], senddata_gb, 7);
				sendto(send_fd, &senddata, sizeof(struct can_frame), 0, addr_yc, addr_size);
			}
			else if( (index > 0) && (index < CAN_GB_INDEX - 1) )
			{
				usleep(300);
				senddata.data[0] = index;
				memcpy(&senddata.data[1], &senddata_gb[index*7], 7);
				sendto(send_fd, &senddata, sizeof(struct can_frame), 0, addr_yc, addr_size);
			}
			else if( index == CAN_GB_INDEX - 1 )
			{
				usleep(300);
				senddata.data[0] = 0x80 | index;
				if( CAN_GB_MOD == 0 )
					memcpy(&senddata.data[1], &senddata_gb[index*7], 6);
				else
				{
					senddata.can_dlc= CAN_GB_MOD+1;
					memcpy(&senddata.data[1], &senddata_gb[index*7], CAN_GB_MOD-1);
				}
				for( int i=0; i < CAN_GB_MAX-1; i++ )
				{
					gb_sum += senddata_gb[i];
				}
				senddata.data[senddata.can_dlc-1] = gb_sum;
				sendto(send_fd, &senddata, sizeof(struct can_frame), 0, addr_yc, addr_size);
			}
        }
	}
}


/* 	CAN1鍙戦�佸甯у寘鏍煎紡鏁版嵁 , data_type涓哄簲绛斿抚绫诲瀷锛堝壇瀵煎ご锛氶仴娴�0x01/骞挎挱0x02锛� */
void senddata_normal_1(int data_type, int send_fd, struct sockaddr *addr_yc, int addr_size)
{
	struct can_frame senddata;
	int gb_sum=0, yc_sum=0;//绮惧瘑瀹氳建骞挎挱/AI閬ユ祴鏁版嵁鍖卻um


	if( data_type == 0x01 )//閬ユ祴鍖�
	{
		senddata.can_id = 0x4e7;
		senddata.can_dlc= 0x08;

		if( count_yc == 0x3FFF )
			count_yc = 1;
		else
			count_yc++;

/* 閬ユ祴鍖呬富瀵煎ご+鍓澶�*/
		senddata_yc[0] = 0x48;
		senddata_yc[1] = 0xb2;
		senddata_yc[2] = ((count_yc >> 8) | 0xc0);
		senddata_yc[3] = (count_yc & 0xff);
		senddata_yc[4] = (CAN_YC_NUM + 1) >> 8;
		senddata_yc[5] = CAN_YC_NUM + 1;
		senddata_yc[6] = 0x01;

/* Z7閬ユ祴閲�10瀛楄妭*/
		senddata_yc[7]  = measure();         //W1:Z7娓╁害
		senddata_yc[8]  = timeout_flag;      //W2:AI宸ヤ綔瓒呮椂缃綅
		senddata_yc[9]  = count_jj_all;      //W3:AI闂存帴鎸囦护鎺ユ敹鎬昏鏁�
		senddata_yc[10] = count_jj;          //W4:TX2闂存帴鎸囦护搴旂瓟璁℃暟
		senddata_yc[11] = count_dg >> 8;     //W5-W6:绮惧瘑瀹氳建tx2搴旂瓟璁℃暟
		senddata_yc[12] = count_dg;
		senddata_yc[13] = count_pcm >> 8;    //W7-W8:楂橀�熶笂琛岄�氶亾鏁版嵁鎺ユ敹璁℃暟
		senddata_yc[14] = count_pcm;
		senddata_yc[15] = count_gtxsd >> 16; //W9-W11:鏁颁紶B閫氶亾鏁版嵁鍙戦�佽鏁�
		senddata_yc[16] = count_gtxsd >> 8;
		senddata_yc[17] = count_gtxsd;
		senddata_yc[18] = count_udpsd >> 8;  //W12-W13:TX2楂橀�熶笂琛屾暟鎹彂閫佽鏁�
		senddata_yc[19] = count_udpsd;
		senddata_yc[20] = count_rs;    	     //W14:TX2楂橀�熶笂琛屾暟鎹潡鎺ユ敹搴旂瓟璁℃暟
		senddata_yc[21] = count_sar >> 16;   //W15-W17:SAR澶勭悊鏁版嵁ZYNQ鎺ユ敹璁℃暟
		senddata_yc[22] = count_sar >> 8;
		senddata_yc[23] = count_sar;

/* TX2閬ユ祴閲�9瀛楄妭:CPU浣跨敤鐜囥�佸唴瀛樺崰鐢ㄧ巼銆佺‖鐩樹娇鐢ㄧ巼銆佹俯搴︺�佸畾杞ˋPP鏄惁寮�鍚�佸畾杞ˋPP杩愯鐘舵�併�乀X2鐘舵�佽鏁般�佷繚鐣�1瀛楄妭銆丄I鐘舵�佹爣璇嗐�乑7绱绉掓暣鏁�*/
		memcpy(&senddata_yc[24], tx2_yc, 8);//鍒濆涓�0xAA
		senddata_yc[32] = mode_flag | tx2_state | change_422;//W26:AI鐘舵�佹爣璇�
		senddata_yc[33] = count_z7;          //W27:Z7绱绉掓暣鏁�

		/* 鍙戦�侀仴娴嬪寘鏁版嵁*/
		for(int index=0; index <CAN_YC_INDEX; index++ )
		{
			if( index == 0)
			{
				senddata.data[0] = 0x40;
				memcpy(&senddata.data[1], senddata_yc, 7);
				sendto(send_fd, &senddata, sizeof(struct can_frame), 0, addr_yc, addr_size);
			}
			else if( (index > 0) && (index < CAN_YC_INDEX - 1) )
			{
				usleep(300);
				senddata.data[0] = index;
				memcpy(&senddata.data[1], &senddata_yc[index*7], 7);
				sendto(send_fd, &senddata, sizeof(struct can_frame), 0, addr_yc, addr_size);
			}
			else if( index == CAN_YC_INDEX - 1 )
			{
				usleep(300);
				senddata.data[0] = 0x80 | index;
				if( CAN_YC_MOD == 0 )
					memcpy(&senddata.data[1], &senddata_yc[index*7], 6);
				else
				{
					senddata.can_dlc= CAN_YC_MOD+1;
					memcpy(&senddata.data[1], &senddata_yc[index*7], CAN_YC_MOD-1);
				}
				for( int i=0; i < CAN_YC_MAX-1; i++ )
				{
					yc_sum += senddata_yc[i];
				}
				senddata.data[senddata.can_dlc-1] = yc_sum;
				sendto(send_fd, &senddata, sizeof(struct can_frame), 0, addr_yc, addr_size);
			}
		}
	}
	else if( data_type == 0x02 )
	{
		senddata.can_id = 0x4cf;
		senddata.can_dlc= 0x08;

		if( count_gb == 0x3FFF )
			count_gb = 1;
		else
			count_gb++;

		senddata_gb[0] = 0x48;
		senddata_gb[1] = 0xb2;
		senddata_gb[2] = ((count_gb >> 8) | 0xc0);
		senddata_gb[3] = (count_gb & 0xff);
		senddata_gb[4] = (CAN_GB_NUM + 1) >> 8;
		senddata_gb[5] = CAN_GB_NUM + 1;
		senddata_gb[6] = 0x02;

		for(int index=0; index <CAN_GB_INDEX; index++ )
		{
			if( index == 0)
			{
				senddata.data[0] = 0x40;
				memcpy(&senddata.data[1], senddata_gb, 7);
				sendto(send_fd, &senddata, sizeof(struct can_frame), 0, addr_yc, addr_size);
			}
			else if( (index > 0) && (index < CAN_GB_INDEX - 1) )
			{
				usleep(300);
				senddata.data[0] = index;
				memcpy(&senddata.data[1], &senddata_gb[index*7], 7);
				sendto(send_fd, &senddata, sizeof(struct can_frame), 0, addr_yc, addr_size);
			}
			else if( index == CAN_GB_INDEX - 1 )
			{
				usleep(300);
				senddata.data[0] = 0x80 | index;
				if( CAN_GB_MOD == 0 )
					memcpy(&senddata.data[1], &senddata_gb[index*7], 6);
				else
				{
					senddata.can_dlc= CAN_GB_MOD+1;
					memcpy(&senddata.data[1], &senddata_gb[index*7], CAN_GB_MOD-1);
				}
				for( int i=0; i < CAN_GB_MAX-1; i++ )
				{
					gb_sum += senddata_gb[i];
				}
				senddata.data[senddata.can_dlc-1] = gb_sum;
				sendto(send_fd, &senddata, sizeof(struct can_frame), 0, addr_yc, addr_size);
			}
		}
	}
}

/* CAN0閬ユ祴銆佸箍鎾帶鍒跺簭鍒楀崟甯ф牎楠屽拰 */
int telemeter_recvsum(struct can_frame *recieve)
{
	int sum=0;

	for(int i=0; i<7; i++)
	  {
		 sum += (*recieve).data[i];
	  }
	sum &= 0xff; //涓诲澶村壇瀵煎ご鏁版嵁鐩稿姞涔嬪悗鍙栦綆瀛楄妭

	return sum;
}

/* CAN1閬ユ祴銆佸箍鎾帶鍒跺簭鍒楀崟甯ф牎楠屽拰 */
int telemeter_recvsum_1(struct can_frame *recieve)
{
	int sum=0;

	for(int i=0; i<7; i++)
	  {
		 sum += (*recieve).data[i];
	  }
	sum &= 0xff; //涓诲澶村壇瀵煎ご鏁版嵁鐩稿姞涔嬪悗鍙栦綆瀛楄妭

	return sum;
}


/* 闂存帴鎸囦护鎺у埗搴忓垪鍗曞抚鏍￠獙鍜� */
int indirect_recvsum(struct can_frame *recieve)
{
	int sum=0;

	for(int i=0; i<5; i++)
	  {
		 sum += (*recieve).data[i];
	  }
	sum &= 0xff;

	return sum;
}


/* 闂存帴鎸囦护搴旂瓟搴忓垪  */
void senddata_unnormal(int send_fd, struct sockaddr *addr_yc, int addr_size)
{
	struct can_frame senddata;

	senddata.can_id = 0x2e4;
	senddata.can_dlc = 0x04;

	senddata.data[0] = 0xb2;
	senddata.data[1] = 0xff;
	senddata.data[2] = 0x20;
	senddata.data[3] = senddata.data[0]+senddata.data[1]+senddata.data[2];
	sendto(send_fd, &senddata, sizeof(struct can_frame), 0, addr_yc, addr_size);
}


/* 鍐呭姟鎸囦护鎺ユ敹鏁版嵁鍖呮牎楠屽拰 */
int action_recvsum(struct can_frame *recieve)
{
	int sum=0;

	for(int i=0; i<7; i++)
	  {
		 sum += (*recieve).data[i];
	  }
	sum &= 0xff; //涓诲澶村壇瀵煎ご鏁版嵁鐩稿姞涔嬪悗鍙栦綆瀛楄妭

	return sum;
}


/* 鍐呭姟鎸囦护搴旂瓟搴忓垪  */
void senddata_action(int send_fd, struct sockaddr *addr_nw, int addr_size, unsigned char action_fdt)
{
	struct can_frame senddata;
    int sum = 0;

	senddata.can_id = 0x2e6;
	senddata.can_dlc = 0x08;

	senddata.data[0] = 0x58;
	senddata.data[1] = 0xb2;
	senddata.data[2] = 0xc0;
	senddata.data[3] = 0x00;
	senddata.data[4] = 0x00;
	senddata.data[5] = 0x01;
	senddata.data[6] = action_fdt;
	for(int i=0; i<7; i++)
	{
	   sum += senddata.data[i];
	}
	senddata.data[7] = sum & 0xff;
	sendto(send_fd, &senddata, sizeof(struct can_frame), 0, addr_nw, addr_size);
}


/* 澶氬寘鏁版嵁鎺ユ敹锛孏NSS骞挎挱鏁版嵁鎺ユ敹 */
void multiple_gnssdata(struct can_frame *recvdata, int type_vId)
{
	static int index_gps=-1, index_bds=-1, flag_gps=66, flag_bds=66;
	static unsigned char *gps_point, *bds_point;

	/* GNSS鏁版嵁甯�:GPS鏁版嵁鎴朆DS鏁版嵁  */
	if( type_vId == GNSS_VID )
	{
		/* 棣栧抚*/
		if( ((((*recvdata).data[0] >> 6 ) & 0x03 ) == 0x01) && ((*recvdata).data[2] == GNSS_VID) )
		{
			if( (*recvdata).data[7] == CAN_GPS_FDT ) //棣栧抚GPS
			{
				flag_gps = 0x05;
				index_gps = (*recvdata).data[0] & 0x3f;
				gps_length = ( ((*recvdata).data[5] << 8 ) | (*recvdata).data[6] ) & 0xffff;  //鑾峰彇鏁版嵁鍖呴暱搴�
				gps_point = gps_recvdata;
				for( int i=0; i<7; i++)
				{
					*gps_point = (*recvdata).data[i+1];
					gps_point ++;
				}
				DBG_MSG("Address---gps_point1 = %p !\n", gps_point);
			}
			else if( (*recvdata).data[7] == CAN_BDS_FDT ) //棣栧抚BDS
			{
				flag_bds = 0x07;
				index_bds = (*recvdata).data[0] & 0x3f;
				bds_length = ( ((*recvdata).data[5] << 8 ) | (*recvdata).data[6] ) & 0xffff;  //鑾峰彇鏁版嵁鍖呴暱搴�
				bds_point = bds_recvdata;
				for( int i=0; i<7; i++)
				{
					*bds_point = (*recvdata).data[i+1];
					bds_point ++;
				}
				DBG_MSG("Address---bds_point1 = %p !\n", bds_point);
			}
		}
		/* 涓棿甯�*/
		else if( (((*recvdata).data[0] >> 6 ) & 0x03 ) == 0 )
		{
			if( flag_gps == 5 )
			{
				if( index_gps == ( ( (*recvdata).data[0] & 0x3f ) - 1) )
				{
					index_gps = (*recvdata).data[0] & 0x3f;
					if( (gps_point > gps_recvdata) && (gps_point < (gps_recvdata + CAN_GNSS_MAX)) )
					{
						for( int i=1; i<8; i++ )
						{
							*gps_point = (*recvdata).data[i];
							gps_point++;
						}
					}
					DBG_MSG("Address---gps_point2 = %p !\n", gps_point);
				}
			}
			if( flag_bds == 7 )
			{
				if( index_bds == ( ( (*recvdata).data[0] & 0x3f ) - 1) )
				{
					index_bds = (*recvdata).data[0] & 0x3f;
					if( (bds_point > bds_recvdata) && (bds_point < (bds_recvdata + CAN_GNSS_MAX)) )
					{
						for( int i=1; i<8; i++ )
						{
							*bds_point = (*recvdata).data[i];
							bds_point++;
						}
					}
					DBG_MSG("Address---bds_point2 = %p !\n", bds_point);
				}
			}
		}
		/* 缁撴潫甯�*/
		else if( (((*recvdata).data[0] >> 6 ) & 0x03 ) == 0x02 )
		{
			if( flag_gps == 5 )
			{
				if( index_gps == ( ( (*recvdata).data[0] & 0x3f ) - 1) )
				{
					index_gps = -1;
					if( (gps_point > gps_recvdata) && (gps_point < (gps_recvdata + CAN_GNSS_MAX)) )
					{
						for( int i=1; i<(*recvdata).can_dlc; i++ )
						{
							*gps_point = (*recvdata).data[i];
							gps_point++;
						}
						gps_recvdlc = gps_point - gps_recvdata;//gps鎺ユ敹鏁版嵁鍖呴暱锛氭暣鍖呮暟鎹煙
						gps_end = 66;//鎺ユ敹瀹屾垚鏍囧織锛孶DP寮�濮嬩紶杈�

					}
					flag_gps = 66;

       				if( count_gps1 < 65535 )
       					count_gps1 ++;
       				else
       					count_gps1 = 1;

					DBG_MSG("Address---gps_point3 = %p !\n", gps_point);
					gps_point = gps_recvdata;
					DBG_MSG("Address---gps_point4 = %p !\n", gps_point);
				}
			}
			if( flag_bds == 7 )
			{
				if( index_bds == (((*recvdata).data[0] & 0x3f) - 1) )
				{
					index_bds = -1;
					if( (bds_point > bds_recvdata) && (bds_point < (bds_recvdata + CAN_GNSS_MAX)) )
					{
						for( int i=1; i<(*recvdata).can_dlc; i++ )
						{
							*bds_point = (*recvdata).data[i];
							bds_point++;
						}
						bds_recvdlc = bds_point - bds_recvdata;//bds鎺ユ敹鏁版嵁鍖呴暱锛氭暣鍖呮暟鎹煙
						bds_end = 66;//鎺ユ敹瀹屾垚鏍囧織锛孶DP寮�濮嬩紶杈�

					}
					flag_bds = 66;

       				if( count_bds1 < 65535 )
       					count_bds1 ++;
       				else
       					count_bds1 = 1;

					DBG_MSG("Address---bds_point3 = %p !\n", bds_point);
					bds_point = bds_recvdata;
					DBG_MSG("Address---bds_point4 = %p !\n", bds_point);
				}
			}
		}
	}
}


/* 澶氬寘鏁版嵁鎺ユ敹锛孏NSS鏄熷巻骞挎挱鏁版嵁鎺ユ敹 */
void multiple_tledata(struct can_frame *recvdata, int type_vId)
{
	static int index_gps=-1, index_bds=-1, flag_gps=66, flag_bds=66;
	static unsigned char *gps_point, *bds_point;

	/* GNSS鏄熷巻鏁版嵁甯�:GPS鏁版嵁鎴朆DS鏁版嵁  */
	if( type_vId == GNSS_VID )
	{
		/* 棣栧抚*/
		if( ((((*recvdata).data[0] >> 6 ) & 0x03 ) == 0x01) && ((*recvdata).data[2] == GNSS_VID) )
		{
			if( (*recvdata).data[7] == CAN_TLEGPS_FDT ) //棣栧抚GPS
			{
				flag_gps = 0x08;
				index_gps = (*recvdata).data[0] & 0x3f;
				tlegps_length = ( ((*recvdata).data[5] << 8 ) | (*recvdata).data[6] ) & 0xffff;  //鑾峰彇鏁版嵁鍖呴暱搴�
				gps_point = tlegps_recvdata;
				for( int i=0; i<7; i++)
				{
					*gps_point = (*recvdata).data[i+1];
					gps_point++;
				}
				DBG_MSG("Address---tlegps_point1 = %p !\n", gps_point);
			}
			else if( (*recvdata).data[7] == CAN_TLEBDS_FDT ) //棣栧抚BDS
			{
				flag_bds = 0x09;
				index_bds = (*recvdata).data[0] & 0x3f;
				tlebds_length = ( ((*recvdata).data[5] << 8 ) | (*recvdata).data[6] ) & 0xffff;  //鑾峰彇鏁版嵁鍖呴暱搴�
				bds_point = tlebds_recvdata;
				for( int i=0; i<7; i++)
				{
					*bds_point = (*recvdata).data[i+1];
					bds_point++;
				}
				DBG_MSG("Address---tlebds_point1 = %p !\n", bds_point);
			}
		}
		/* 涓棿甯�*/
		else if( (((*recvdata).data[0] >> 6 ) & 0x03 ) == 0 )
		{
			if( flag_gps == 8 )
			{
				if( index_gps == ( ( (*recvdata).data[0] & 0x3f ) - 1) )
				{
					index_gps = (*recvdata).data[0] & 0x3f;
					if( (gps_point > tlegps_recvdata) && (gps_point < (tlegps_recvdata + CAN_TLE_MAX)) )
					{
						for( int i=1; i<8; i++ )
						{
							*gps_point = (*recvdata).data[i];
							gps_point++;
						}
					}
					DBG_MSG("Address---gps_point2 = %p !\n", gps_point);
				}
			}
			if( flag_bds == 9 )
			{
				if( index_bds == ( ( (*recvdata).data[0] & 0x3f ) - 1) )
				{
					index_bds = (*recvdata).data[0] & 0x3f;
					if( (bds_point > tlebds_recvdata) && (bds_point < (tlebds_recvdata + CAN_TLE_MAX)) )
					{
						for( int i=1; i<8; i++ )
						{
							*bds_point = (*recvdata).data[i];
							bds_point++;
						}
					}
					DBG_MSG("Address---bds_point2 = %p !\n", bds_point);
				}
			}
		}
		/* 缁撴潫甯�*/
		else if( (((*recvdata).data[0] >> 6 ) & 0x03 ) == 0x02 )
		{
			if( flag_gps == 8 )
			{
				if( index_gps == ( ( (*recvdata).data[0] & 0x3f ) - 1) )
				{
					index_gps = -1;
					if( (gps_point > tlegps_recvdata) && (gps_point < (tlegps_recvdata + CAN_TLE_MAX)) )
					{
						for( int i=1; i<(*recvdata).can_dlc; i++ )
						{
							*gps_point = (*recvdata).data[i];
							gps_point++;
						}
						tlegps_recvdlc = gps_point - tlegps_recvdata;//gps鎺ユ敹鏁版嵁鍖呴暱锛氭暣鍖呮暟鎹煙
						tlegps_end = 66;//鎺ユ敹瀹屾垚鏍囧織锛孶DP寮�濮嬩紶杈�

					}
					flag_gps = 66;

       				if( count_tlegps1 < 65535 )
       					count_tlegps1 ++;
       				else
       					count_tlegps1 = 1;

					DBG_MSG("Address---tlegps_point3 = %p !\n", gps_point);
					gps_point = tlegps_recvdata;
					DBG_MSG("Address---tlegps_point4 = %p !\n", gps_point);
				}
			}
			if( flag_bds == 9 )
			{
				if( index_bds == (((*recvdata).data[0] & 0x3f) - 1) )
				{
					index_bds = -1;
					if( (bds_point > tlebds_recvdata) && (bds_point < (tlebds_recvdata + CAN_TLE_MAX)) )
					{
						for( int i=1; i<(*recvdata).can_dlc; i++ )
						{
							*bds_point = (*recvdata).data[i];
							bds_point++;
						}
						tlebds_recvdlc = bds_point - tlebds_recvdata;//bds鎺ユ敹鏁版嵁鍖呴暱锛氭暣鍖呮暟鎹煙
						tlebds_end = 66;//鎺ユ敹瀹屾垚鏍囧織锛孶DP寮�濮嬩紶杈�

					}
					flag_bds = 66;

       				if( count_tlebds1 < 65535 )
       					count_tlebds1 ++;
       				else
       					count_tlebds1 = 1;

					DBG_MSG("Address---tlebds_point3 = %p !\n", bds_point);
					bds_point = bds_recvdata;
					DBG_MSG("Address---tlebds_point4 = %p !\n", bds_point);
				}
			}
		}
	}
}


/* 澶氬寘鏁版嵁鎺ユ敹锛屾槦鏁忓箍鎾暟鎹帴鏀� */
void multiple_acsdata(struct can_frame *recvdata, int type_vId)
{
	static int index_acs=-1;
    static unsigned char *acs_point;

    /* 棣栧抚*/
    if( type_vId == ACS_VID )
    {
    	if( ((((*recvdata).data[0] >> 6 ) & 0x03 ) == 0x01) && ((*recvdata).data[2] == ACS_VID) )//棣栧抚
		{
			if( (*recvdata).data[7]  == CAN_ACS_FDT ) //ACS鏈夋晥鏁版嵁1+244
			{
				index_acs = (*recvdata).data[0] & 0x3f;//index璁℃暟
				acs_length = ( ((*recvdata).data[5] << 8 ) | (*recvdata).data[6] ) & 0xffff;  //鑾峰彇鏁版嵁鍖呴暱搴�
				acs_point = acs_recvdata;
				for( int i=0; i<7; i++)
				{
					*acs_point = (*recvdata).data[i+1];
					acs_point++;
				}
				DBG_MSG("Address---acs_point1: %p !\n" , acs_point);
			}
		}
    	/* 涓棿甯�*/
		else if( (((*recvdata).data[0] >> 6 ) & 0x03 ) == 0 )
		{
			if( index_acs == (((*recvdata).data[0] & 0x3f) - 1) )
			{
				index_acs = (*recvdata).data[0] & 0x3f;
				if( (acs_point > acs_recvdata) && (acs_point < (acs_recvdata + CAN_ACS_MAX)) )
				{
					for( int i=1; i<8; i++ )
					{
						*acs_point = (*recvdata).data[i];
						acs_point++;
					}
				}
				DBG_MSG("Address---acs_point2: %p !\n" , acs_point);
			}
		}
    	/* 缁撴潫甯�*/
		else if( (((*recvdata).data[0] >> 6 ) & 0x03 ) == 0x02 )
		{
			if( index_acs == ( ((*recvdata).data[0] & 0x3f) - 1) )
			{
				index_acs = -1;
				if( (acs_point > acs_recvdata) && (acs_point < (acs_recvdata + CAN_ACS_MAX)) )
				{
					for( int i=1; i<(*recvdata).can_dlc; i++ )
					{
						*acs_point = (*recvdata).data[i];
						acs_point++;
					}
					acs_recvdlc = acs_point - acs_recvdata;//acs鎺ユ敹鏁版嵁鍖呴暱锛氬壇瀵煎ご+鏈夋晥鏁版嵁
					acs_end = 66;//鎺ユ敹瀹屾垚鏍囧織锛孶DP寮�濮嬩紶杈�

       				if( count_acs1 < 65535 )
       					count_acs1 ++;
       				else
       					count_acs1 = 1;
				}
				DBG_MSG("Address---acs_point3: %p !\n" , acs_point);
				acs_point = acs_recvdata;
				DBG_MSG("Address---acs_point4: %p !\n" , acs_point);
			}
		}
    }
}


/* 澶氬寘鏁版嵁鎺ユ敹锛岃建閬撳箍鎾暟鎹帴鏀� */
void multiple_orbitdata(struct can_frame *recvdata, int type_vId)
{
	static int index_acs=-1;
    static unsigned char *acs_point;

    /* 棣栧抚*/
    if( type_vId == ACS_VID )
    {
    	if( ((((*recvdata).data[0] >> 6 ) & 0x03 ) == 0x01) && ((*recvdata).data[2] == ACS_VID) )//棣栧抚
		{
			if( (*recvdata).data[7]  == CAN_ORBIT_FDT ) //ORBIT鏈夋晥鏁版嵁1+55
			{
				index_acs = (*recvdata).data[0] & 0x3f;//index璁℃暟
				orbit_length = ( ((*recvdata).data[5] << 8 ) | (*recvdata).data[6] ) & 0xffff;  //鑾峰彇鏁版嵁鍖呴暱搴�
				acs_point = orbit_recvdata;
				for( int i=0; i<7; i++)
				{
					*acs_point = (*recvdata).data[i+1];
					acs_point++;
				}
				DBG_MSG("Address---acs_orbit_point1: %p !\n" , acs_point);
			}
		}
    	/* 涓棿甯�*/
		else if( (((*recvdata).data[0] >> 6 ) & 0x03 ) == 0 )
		{
			if( index_acs == (((*recvdata).data[0] & 0x3f) - 1) )
			{
				index_acs = (*recvdata).data[0] & 0x3f;
				if( (acs_point > orbit_recvdata) && (acs_point < (orbit_recvdata + CAN_ORBIT_MAX)) )
				{
					for( int i=1; i<8; i++ )
					{
						*acs_point = (*recvdata).data[i];
						acs_point++;
					}
				}
				DBG_MSG("Address---acs_point2: %p !\n" , acs_point);
			}
		}
    	/* 缁撴潫甯�*/
		else if( (((*recvdata).data[0] >> 6 ) & 0x03 ) == 0x02 )
		{
			if( index_acs == ( ((*recvdata).data[0] & 0x3f) - 1) )
			{
				index_acs = -1;
				if( (acs_point > orbit_recvdata) && (acs_point < (orbit_recvdata + CAN_ORBIT_MAX)) )
				{
					for( int i=1; i<(*recvdata).can_dlc; i++ )
					{
						*acs_point = (*recvdata).data[i];
						acs_point++;
					}
					orbit_recvdlc = acs_point - orbit_recvdata;//acs鎺ユ敹杞ㄩ亾鏁版嵁鍖呴暱锛氬壇瀵煎ご+鏈夋晥鏁版嵁
					orbit_end = 66;//鎺ユ敹瀹屾垚鏍囧織锛孶DP寮�濮嬩紶杈�

       				if( count_orbit1 < 65535 )
       					count_orbit1 ++;
       				else
       					count_orbit1 = 1;
				}
				DBG_MSG("Address---acs_orbit_point3: %p !\n" , acs_point);
				acs_point = orbit_recvdata;
				DBG_MSG("Address---acs_orbit_point4: %p !\n" , acs_point);
			}
		}
    }
}








/* 娴嬭瘯骞挎挱鏁版嵁鍐欏叆txt鏂囦欢  */
void acsdata_write(const char *filename, unsigned char *data, int len)
{
	    FILE* fptr = fopen(filename , "a");
		if (fptr == NULL)
		{
		    puts("Fail to open file!");
		    return -1;
		}
//		for(int i=0; i<len; i++)
//		{
//
//			fprintf(fptr, "%02x ", data[i]);
//		}



	    for(int i=0; i<len; i++)
	    {
	        if(((i%30) == 0)&&(i>0))
	            fprintf(fptr, "\n");
	        fprintf(fptr, "%02x\t", data[i]);
	    }
	    fprintf(fptr, "\n");
//	    fprintf(fptr, "\n");
	    fclose(fptr);
}




/*************************
 *
 *
 *************************/
/* CAN0绾跨▼  */
void *thread_can_0(void *arg)
{
	/* CAN0鍒濆鍖栬繃绋嬪彉閲�  */
    int fd, ret, flag, addr_length;
    struct sockaddr_can addr;
    struct ifreq ifr;
    struct can_frame frame;
    struct can_filter rfilter;//杩囨护鍣�

    socklen_t socket_len = sizeof(struct sockaddr_can);
    addr_length = sizeof(addr);


    /* Create a socket with PF_CAN family, SOCK_RAW and CAN_RAW protocol */
    fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
    if (fd < 0) {
        ERR_MSG("Open socket failed!\n");
        return NULL;
    }

    /* Use can0 --灏�"can0"鏀规垚"can1灏卞彲浠ョ敤can1,鍏朵粬涓嶇敤鍙�" */
    strcpy((char *)(ifr.ifr_name), "can0");

    /* Get information */
    ret = ioctl(fd, SIOCGIFINDEX, &ifr);
    if (ret != 0) {
        ERR_MSG("SIOCGIFINDEX failed! ret:%d\n", ret);
        close(fd);
        return NULL;
    }
    DBG_MSG("can0 can_ifindex = %02x\n",ifr.ifr_ifindex);

    /***娴嬭瘯***/
    memset(&addr, 0, sizeof(addr));

    /* Disable loopback */
    flag = 0;
    ret = setsockopt(fd, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &flag, sizeof(flag));
    if (ret != 0) {
        ERR_MSG("Set loopback disable failed! ret:%d\n", ret);
        close(fd);
        return NULL;
    }
    DBG_MSG("Set can0 loopback disable\n");

    /* Disable receiving own message */
    flag = 0;
    ret = setsockopt(fd, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
               &flag, sizeof(flag));
    if (ret != 0) {
        ERR_MSG("Disable receiving own message failed! ret:%d\n", ret);
        close(fd);
        return NULL;
    }
    DBG_MSG("Disable receiving own message\n");

    /* Use AF_CAN protocol family */
    addr.can_family = AF_CAN;
    addr.can_ifindex = ifr.ifr_ifindex;

    /* Binding socket */
    ret = bind(fd, (struct sockaddr*)&addr, sizeof(addr));
    if (ret != 0) {
        ERR_MSG("Bind socket failed! ret:%d\n", ret);
        close(fd);
        return NULL;
    }
    DBG_MSG("Bind can0 socket\n");

    /* Filter璁剧疆  */
    rfilter.can_id = 0x4e2;
    rfilter.can_mask = 0x68;
    setsockopt(fd, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
    fd_a = fd;
//    printf("fd_a : %d\n", fd_a);


    while (1) {
    	/* 浠巆an鎺ュ彛鎺ユ敹鏁版嵁  */
        ret = recvfrom(fd, &frame, sizeof(struct can_frame), 0, (struct sockaddr *)&addr, &socket_len);//0鏀规垚MSG_DONTWAIT
        DBG_MSG("can0 RECV\n");
        /* 鍒ゆ柇鎺ユ敹鏁版嵁绫诲瀷涓鸿疆璇㈤仴娴嬨�佽疆璇㈠箍鎾�侀棿鎺ユ寚浠ゃ�佸唴鍔℃寚浠ゃ�丟NSS骞挎挱銆佸Э鎺ф槦鏁忓箍鎾紝鍏辫6绫绘暟鎹寘  */
        if (ret > 0)
          {
        	//ceshi
        	if( test_id != frame.can_id )
        	{
        		test_id = frame.can_id;
        		printf("A_frame.can_id : %x\n", test_id);
        	}


        	/* 鎺ユ敹鍗曞寘鏁版嵁  */
        	if( (frame.can_id & 0x01) == CAN_SINGLE )
        	{
        		/* 杞閬ユ祴銆侀仴娴嬪箍鎾�  */
        		if( (frame.can_id & 0x7ff) == aican_ID.telemeter_id )
        		{
                    if( frame.can_dlc == 0x08 )
                    {
                    	if( (frame.data[0] == 0x48) && (frame.data[1] == 0xb2) && (frame.data[7] == telemeter_recvsum(&frame)) )
                        {
                			usleep(200);
                    		/* 杞閬ユ祴  */
                    		if( frame.data[6] == CAN_YC_FDT )
                    		{
                    			senddata_normal(CAN_YC_FDT, fd, (struct sockaddr*)&addr, addr_length);
                    			count_z7 ++;//z7绱绉掕鏁�
                    		}
                    		/* 杞骞挎挱  */
                    		else if( (frame.data[6] == CAN_GB_FDT) && (mode_flag == 0) )
                    		{
                    			senddata_normal(CAN_GB_FDT, fd, (struct sockaddr*)&addr, addr_length);
                    			flag_orbit = 66;


                    			/* 鎵撳嵃绮惧瘑瀹氳建骞挎挱鏁版嵁*/
//                    			printf("111111Orbit Send_Message :\n");
//                    			for(int i=0; i < 62; i++ )
//                            	{
//                    				if(((i%16) == 0)&&(i>0))
//										printf("\n");
//                    				printf("%02x\t", senddata_gb[i+7]);
//                            	}
//                            	printf("\n");

                            	/* 绮惧瘑瀹氳建鏍囧織鍙橀噺锛�1s鏌ヨ涓�娆�*/
                    		}
                    	}
                    	else
                    		ERR_MSG("Error0 SUM: W7 = %02x  SUM = %02x ! \n", frame.data[7], telemeter_recvsum(&frame));
                    }
                    else
                    	ERR_MSG("Error0 DLC: %d ! \n", frame.can_dlc);
        		}
        		/* 闂存帴鎸囦护  */
        		else if( (frame.can_id & 0x7ff) == aican_ID.indirect_id )
        		{
        			if( frame.can_dlc == 0x06 )
        			{
        				if( (frame.data[0] == 0x20) && (frame.data[1] == 0xb2) && (frame.data[5] == indirect_recvsum(&frame)) )
        				{
        					usleep(200);
        					senddata_unnormal(fd, (struct sockaddr*)&addr, addr_length);//闂存帴鎸囦护搴旂瓟
        					for( int i=0; i<CAN_INDIRECT; i++ )
        					{
        						indirect_data[i] = frame.data[i+1];//闂存帴鎸囦护瀛樺叆鏁扮粍杞彂UDP
        					}
        					if( count_jj_all < 255 )
            					count_jj_all ++;
    						else
    							count_jj_all = 1;
        					indirect_end = 66;//闂存帴鎸囦护鎺ユ敹瀹屾垚鏍囧織锛屽叏灞�鍙橀噺
        					DBG_MSG("CAN0_indirect------TESTING = %02x %02x \n", indirect_data[1], indirect_data[2]);//鎵撳嵃
        				}
        			}
        		}
        		/* 鍐呭姟鎸囦护*/
        		else if( (frame.can_id & 0x7ff) == aican_ID.action_id )
        		{
                    if( frame.can_dlc == 0x08 )
                    {
                    	if( (frame.data[0] == 0x58) && (frame.data[1] == 0xb2) && (frame.data[7] == action_recvsum(&frame)) )
                        {
                    		usleep(200);
                    		/*蹇冭烦娴嬭瘯锛屽彧闇�搴旂瓟*/
                    		if( frame.data[6] == 0xf1 )
                    		{
                    			senddata_action(fd, (struct sockaddr*)&addr, addr_length, 0xf1);
                    			DBG_MSG("CAN0_heart------TESTING\n");
                    		}
                    		/*A绾垮浣�*/
                    		else if( frame.data[6] == 0xfa )
                    		{
                    			senddata_action(fd, (struct sockaddr*)&addr, addr_length, 0xfa);
//								system("ip link set can0 down");
//								system("ip link set can0 up");
                    			DBG_MSG("CAN0_A_Reset------TESTING\n");
                    		}
                    		/*B绾垮浣�*/
                    		else if( frame.data[6] == 0xfb )
                    		{
                    			senddata_action(fd, (struct sockaddr*)&addr, addr_length, 0xfb);
//								system("ip link set can1 down");
//								system("ip link set can1 up");
								DBG_MSG("CAN0_B_Reset------TESTING\n");
                    		}
            				if( count_nw < 255 )
            					count_nw ++;
    						else
    							count_nw = 1;
                    	}
                    }
        		}
        	}
        	/* 鎺ユ敹澶氬寘鏁版嵁  */
        	else if( (frame.can_id & 0x01) == CAN_MULTIPLE )
        	{
        		/* GNSS骞挎挱鏁版嵁  */
				if( ((frame.can_id & 0x7ff) == aican_ID.gnss_id) )
				{
					multiple_gnssdata(&frame, 0x81);
				}
				/* GNSS鏄熷巻骞挎挱鏁版嵁  */
				else if( ((frame.can_id & 0x7ff) == aican_ID.tle_id) )
				{
					multiple_tledata(&frame, 0x81);
				}
				/* 濮挎帶鏄熸晱骞挎挱鏁版嵁  */
//				else if( ((frame.can_id & 0x7ff) == aican_ID.starsensor_id) )
//				{
//					multiple_acsdata(&frame, 0x04);
//				}
				/* 濮挎帶杞ㄩ亾骞挎挱鏁版嵁  */
//				else if( ((frame.can_id & 0x7ff) == aican_ID.orbit_id) )
//				{
//					multiple_orbitdata(&frame, 0x04);
////					acs_txt = 66;/*************6666*********/
//					DBG_MSG("ACS datas = %d!\n",acs_txt);
//				}
        	}
          }
       }

    return NULL;
}




/* CAN1绾跨▼ */
void *thread_can_1(void *arg)
{
	/* CAN1鍒濆鍖栬繃绋嬪彉閲�  */
	    int fd, ret, flag, i, addr_length;
	    struct sockaddr_can addr;
	    struct ifreq ifr;
	    struct can_frame frame;
	    struct can_filter rfilter;//杩囨护鍣�

	    socklen_t socket_len = sizeof(struct sockaddr_can);
	    addr_length = sizeof(addr);


	    /* Create a socket with PF_CAN family, SOCK_RAW and CAN_RAW protocol */
	    fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
	    if (fd < 0) {
	        ERR_MSG("Open socket failed1!\n");
	        return NULL;
	    }

	    /* Use can0 --灏�"can0"鏀规垚"can1灏卞彲浠ョ敤can1,鍏朵粬涓嶇敤鍙�" */
	    strcpy((char *)(ifr.ifr_name), "can1");

	    /* Get information */
	    ret = ioctl(fd, SIOCGIFINDEX, &ifr);
	    if (ret != 0) {
	        ERR_MSG("SIOCGIFINDEX failed1! ret:%d\n", ret);
	        close(fd);
	        return NULL;
	    }
	    DBG_MSG("can1 can_ifindex = %02x\n",ifr.ifr_ifindex);

	    /* Disable loopback */
	    flag = 0;
	    ret = setsockopt(fd, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &flag, sizeof(flag));
	    if (ret != 0) {
	        ERR_MSG("Set loopback disable failed1! ret:%d\n", ret);
	        close(fd);
	        return NULL;
	    }
	    DBG_MSG("Set can1 loopback disable\n");

	    /* Disable receiving own message */
	    flag = 0;
	    ret = setsockopt(fd, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
	               &flag, sizeof(flag));
	    if (ret != 0) {
	        ERR_MSG("Disable receiving own message failed1! ret:%d\n", ret);
	        close(fd);
	        return NULL;
	    }
	    DBG_MSG("Disable receiving own message1\n");

	    /* Use AF_CAN protocol family */
	    addr.can_family = AF_CAN;
	    addr.can_ifindex = ifr.ifr_ifindex;

	    /* Binding socket */
	    ret = bind(fd, (struct sockaddr*)&addr, sizeof(addr));
	    if (ret != 0) {
	        ERR_MSG("Bind socket failed1! ret:%d\n", ret);
	        close(fd);
	        return NULL;
	    }
	    DBG_MSG("Bind can1 socket\n");

	    /* Filter璁剧疆  */
	    rfilter.can_id = 0x4e2;
	    rfilter.can_mask = 0x68;
	    setsockopt(fd, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
	    fd_b = fd;
//	    printf("fd_b : %d\n", fd_b);

	    while (1) {
	    	/* 浠巆an鎺ュ彛鎺ユ敹鏁版嵁  */
	        ret = recvfrom(fd, &frame, sizeof(struct can_frame), 0, (struct sockaddr *)&addr, &socket_len);
	        /* 鍒ゆ柇鎺ユ敹鏁版嵁绫诲瀷涓鸿疆璇㈤仴娴嬨�佽疆璇㈠箍鎾�侀棿鎺ユ寚浠ゃ�佸唴鍔℃寚浠ゃ�丟NSS骞挎挱銆佸Э鎺ф槦鏁忓箍鎾紝鍏辫6绫绘暟鎹寘  */
	        if (ret > 0)
	          {
	        	//ceshi

//	        	if( test_id != frame.can_id )
//	        	{
//	        		test_id = frame.can_id;
//	        		printf("B_frame.can_id : %x\n", test_id);
//	        	}




	        	/* 鎺ユ敹鍗曞寘鏁版嵁  */
	        	if( (frame.can_id & 0x01) == CAN_SINGLE )
	        	{
	        		/* 杞閬ユ祴銆侀仴娴嬪箍鎾�  */
	        		if( (frame.can_id & 0x7ff) == aican_ID.telemeter_id )
	        		{
	                    if( frame.can_dlc == 0x08 )
	                    {
	                    	if( (frame.data[0] == 0x48) && (frame.data[1] == 0xb2) && (frame.data[7] == telemeter_recvsum_1(&frame)) )
	                        {
	                    		usleep(200);
	                    		/* 杞閬ユ祴  */
	                    		if( frame.data[6] == CAN_YC_FDT )
	                    		{
	                    			senddata_normal_1(CAN_YC_FDT, fd, (struct sockaddr*)&addr, addr_length);
	                    			count_z7 ++;//z7绱绉掕鏁�
	                    		}
	                    		/* 杞骞挎挱  */
	                    		else if( (frame.data[6] == CAN_GB_FDT) && (mode_flag == 0) )
	                    		{
	                    			senddata_normal_1(CAN_GB_FDT, fd, (struct sockaddr*)&addr, addr_length);
	                    			flag_orbit = 66;//绮惧瘑瀹氳建鏍囧織鍙橀噺锛�1s鏌ヨ涓�娆�

	                    			/* 鎵撳嵃绮惧瘑瀹氳建骞挎挱鏁版嵁*/
//	                    			printf("111111Orbit Send_Message :\n");
//	                    			for(int i=0; i < 62; i++ )
//	                            	{
//	                    				if(((i%16) == 0)&&(i>0))
//											printf("\n");
//	                    				printf("%02x\t", senddata_gb[i+7]);
//	                            	}
//	                            	printf("\n");


	                    		}
	                    	}
	                    	else
	                    		ERR_MSG("Error1 SUM: W7 = %02x  SUM = %02x ! \n", frame.data[7], telemeter_recvsum_1(&frame));
	                    }
	                    else
	                    	ERR_MSG("Error1 DLC: %d ! \n", frame.can_dlc);
	        		}
	        		/* 闂存帴鎸囦护  */
	        		else if( (frame.can_id & 0x7ff) == aican_ID.indirect_id )
	        		{
	        			if( frame.can_dlc == 0x06 )
	        			{
	        				if( (frame.data[0] == 0x20) && (frame.data[1] == 0xb2) && (frame.data[5] == indirect_recvsum(&frame)) )
	        				{
	        					usleep(200);
	        					senddata_unnormal(fd, (struct sockaddr*)&addr, addr_length);//闂存帴鎸囦护搴旂瓟
	        					for( i=0; i<CAN_INDIRECT; i++ )
	        					{
	        						indirect_data[i] = frame.data[i+1];//闂存帴鎸囦护瀛樺叆鏁扮粍 杞彂UDP
	        					}
	        					if( count_jj_all < 255 )
	            					count_jj_all ++;
	    						else
	    							count_jj_all = 1;
	        					indirect_end = 66;//闂存帴鎸囦护鎺ユ敹瀹屾垚鏍囧織锛屽叏灞�鍙橀噺
	        					DBG_MSG("CAN1_indirect------TESTING = %02x %02x \n", indirect_data[1], indirect_data[2]);
	        				}
	        			}
	        		}
	        		/* 鍐呭姟鎸囦护 */
	        		else if( (frame.can_id & 0x7ff) == aican_ID.action_id )
	        		{
	                    if( frame.can_dlc == 0x08 )
	                    {
	                    	if( (frame.data[0] == 0x58) && (frame.data[1] == 0xb2) && (frame.data[7] == action_recvsum(&frame)) )
	                        {
	                    		usleep(200);
	                    		/* 蹇冭烦娴嬭瘯锛屽彧闇�搴旂瓟*/
	                    		if( frame.data[6] == 0xf1 )
	                    		{
	                    			senddata_action(fd, (struct sockaddr*)&addr, addr_length, 0xf1);
	                    			DBG_MSG("CAN1_heart------TESTING\n");
	                    		}
	                    		/*A绾垮浣�*/
	                    		else if( frame.data[6] == 0xfa )
	                    		{
	                    			senddata_action(fd, (struct sockaddr*)&addr, addr_length, 0xfa);
//									system("ip link set can0 down");
//									usleep(1000*100);
//									system("ip link set can0 up");
	                    			DBG_MSG("CAN1_A_Reset------TESTING\n");
	                    		}
	                    		/*B绾垮浣�*/
	                    		else if( frame.data[6] == 0xfb )
	                    		{
	                    			senddata_action(fd, (struct sockaddr*)&addr, addr_length, 0xfb);
//									system("ip link set can1 down");
//									system("ip link set can1 up");
									DBG_MSG("CAN1_B_Reset------TESTING\n");
	                    		}
	            				if( count_nw < 255 )
	            					count_nw ++;
	    						else
	    							count_nw = 1;
	                    	}
	                    }
	        		}
	        	}
	        	/* 鎺ユ敹澶氬寘鏁版嵁  */
	        	else if( (frame.can_id & 0x01) == CAN_MULTIPLE )
	        	{
	        		/* GNSS骞挎挱鏁版嵁  */
					if( ((frame.can_id & 0x7ff) == aican_ID.gnss_id) )
					{
						multiple_gnssdata(&frame, 0x81);
					}
					/* GNSS鏄熷巻骞挎挱鏁版嵁  */
					else if( ((frame.can_id & 0x7ff) == aican_ID.tle_id) )
					{
						multiple_tledata(&frame, 0x81);
					}
					/* 濮挎帶鏄熸晱骞挎挱鏁版嵁  */
//					else if( ((frame.can_id & 0x7ff) == aican_ID.starsensor_id) )
//					{
//						multiple_acsdata(&frame, 0x04);
//					}
					/* 濮挎帶杞ㄩ亾骞挎挱鏁版嵁  */
//					else if( ((frame.can_id & 0x7ff) == aican_ID.orbit_id) )
//					{
//						multiple_orbitdata(&frame, 0x04);
////					    acs_txt = 66;/*************6666*********/
//					    DBG_MSG("ACS datas = %d!\n",acs_txt);
//					}
	        	}
	          }
	       }

	    return NULL;
}





























/* CAN2绾跨▼  */
void *thread_can_2(void *arg)
{
	/* CAN0鍒濆鍖栬繃绋嬪彉閲�  */
    int ret, flag, addr_length;
    struct sockaddr_can addr;
    struct ifreq ifr;
    struct can_frame frame;
    struct can_filter rfilter[2];//杩囨护鍣�

    socklen_t socket_len = sizeof(struct sockaddr_can);
    addr_length = sizeof(addr_can0);

    /* Create a socket with PF_CAN family, SOCK_RAW and CAN_RAW protocol */
    fd_2 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
    if (fd_2 < 0) {
        ERR_MSG("Open socket failed!\n");
        return NULL;
    }

    /* Use AF_CAN protocol family */
    addr.can_family = AF_CAN;
    addr.can_ifindex = 0;
    /* Binding socket */
    ret = bind(fd_2, (struct sockaddr*)&addr, sizeof(addr));
    if (ret != 0) {
        ERR_MSG("Bind socket failed! ret:%d\n", ret);
        close(fd_2);
        return NULL;
    }
    DBG_MSG("Bind can0 socket\n");


    /* Use can0 */
    strcpy((char *)(ifr.ifr_name), "can0");
    /* Get information */
    ret = ioctl(fd_2, SIOCGIFINDEX, &ifr);
    if (ret != 0) {
        ERR_MSG("SIOCGIFINDEX failed! ret:%d\n", ret);
        close(fd_2);
        return NULL;
    }
    DBG_MSG("can0 can_ifindex = %02x\n",ifr.ifr_ifindex);
    /* Use AF_CAN protocol family */
    addr_can0.can_family = AF_CAN;
    addr_can0.can_ifindex = ifr.ifr_ifindex;


    /* Use can1 */
    strcpy((char *)(ifr.ifr_name), "can1");
    /* Get information */
    ret = ioctl(fd_2, SIOCGIFINDEX, &ifr);
    if (ret != 0) {
        ERR_MSG("SIOCGIFINDEX failed! ret:%d\n", ret);
        close(fd_2);
        return NULL;
    }
    DBG_MSG("can1 can_ifindex = %02x\n",ifr.ifr_ifindex);
    /* Use AF_CAN protocol family */
    addr_can1.can_family = AF_CAN;
    addr_can1.can_ifindex = ifr.ifr_ifindex;


    /***娴嬭瘯***/
    memset(&addr, 0, sizeof(addr));

    /* Disable loopback */
    flag = 0;
    ret = setsockopt(fd_2, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &flag, sizeof(flag));
    if (ret != 0) {
        ERR_MSG("Set loopback disable failed! ret:%d\n", ret);
        close(fd_2);
        return NULL;
    }
    DBG_MSG("Set can0 loopback disable\n");

    /* Disable receiving own message */
    flag = 0;
    ret = setsockopt(fd_2, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
               &flag, sizeof(flag));
    if (ret != 0) {
        ERR_MSG("Disable receiving own message failed! ret:%d\n", ret);
        close(fd_2);
        return NULL;
    }
    DBG_MSG("Disable receiving own message\n");


    /* Filter璁剧疆  */
    rfilter[0].can_id = 0x4e2;
    rfilter[0].can_mask = 0x68;//姝ｅ父鎺ユ敹CAN鎬荤嚎鏁版嵁
    rfilter[1].can_id = 0x4e2;
    rfilter[1].can_mask = 0x1f8;//浠呮帴鏀禖AN鎬荤嚎杞
    ret = setsockopt(fd_2, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
    if (ret != 0)
    {
            ERR_MSG("Filter socket failed! ret:%d\n", ret);
            close(fd_2);
            return NULL;
            DBG_MSG("Disable Filter socket!\n");
    }
    else
    	can_init_ready = 66;//CAN绾跨▼鍒濆鍖栫粨鏉熸爣蹇�

    printf("fd_2 : %d\n", fd_2);


    while (1) {
    	  /* 浠巆an鎺ュ彛鎺ユ敹鏁版嵁  */
          ret = recvfrom(fd_2, &frame, sizeof(struct can_frame), 0, (struct sockaddr *)&addr_can0, &socket_len);//0鏀规垚MSG_DONTWAIT

          /* 鍒ゆ柇鎺ユ敹鏁版嵁绫诲瀷涓鸿疆璇㈤仴娴嬨�佽疆璇㈠箍鎾�侀棿鎺ユ寚浠ゃ�佸唴鍔℃寚浠ゃ�丟NSS骞挎挱銆佸Э鎺ф槦鏁忓箍鎾紝鍏辫6绫绘暟鎹寘  */
          if (ret > 0)
          {
        	/* 鎺ユ敹鍗曞寘鏁版嵁  */
        	if( (frame.can_id & 0x01) == CAN_SINGLE )
        	{
        		/* 杞閬ユ祴銆侀仴娴嬪箍鎾�  */
        		if( (frame.can_id & 0x7ff) == aican_ID.telemeter_id )
        		{
                    if( frame.can_dlc == 0x08 )
                    {
                    	if( (frame.data[0] == 0x48) && (frame.data[1] == 0xb2) && (frame.data[7] == telemeter_recvsum(&frame)) )
                        {
                			usleep(200);
                    		/* 杞閬ユ祴  */
                    		if( frame.data[6] == CAN_YC_FDT )
                    		{
                    			senddata_normal(CAN_YC_FDT, fd_2, (struct sockaddr*)&addr_can0, addr_length);
                    		}
                    		/* 杞骞挎挱  */
                    		else if( (frame.data[6] == CAN_GB_FDT) && (mode_flag == 0) )
                    		{
                    			senddata_normal(CAN_GB_FDT, fd_2, (struct sockaddr*)&addr_can0, addr_length);
                    			flag_orbit = 66;
                    			count_z7 ++;//z7绱绉掕鏁�

                    			/* 鎵撳嵃绮惧瘑瀹氳建骞挎挱鏁版嵁*/
//                    			printf("Orbit Message :\n");
//                    			for(int i=0; i < 62; i++ )
//                            	{
//                    				if(((i%16) == 0)&&(i>0))
//										printf("\n");
//                    				printf("%02x\t", senddata_gb[i+7]);
//                            	}
//                            	printf("\n");

                            	/* 绮惧瘑瀹氳建鏍囧織鍙橀噺锛�1s鏌ヨ涓�娆�*/
                    		}
                    	}
                    	else
                    		ERR_MSG("Error0 SUM: W7 = %02x  SUM = %02x ! \n", frame.data[7], telemeter_recvsum(&frame));
                    }
                    else
                    	ERR_MSG("Error0 DLC: %d ! \n", frame.can_dlc);
        		}
        		/* 闂存帴鎸囦护  */
        		else if( (frame.can_id & 0x7ff) == aican_ID.indirect_id )
        		{
        			if( frame.can_dlc == 0x06 )
        			{
        				if( (frame.data[0] == 0x20) && (frame.data[1] == 0xb2) && (frame.data[5] == indirect_recvsum(&frame)) )
        				{
        					usleep(200);
        					senddata_unnormal(fd_2, (struct sockaddr*)&addr_can0, addr_length);//闂存帴鎸囦护搴旂瓟
        					for( int i=0; i<CAN_INDIRECT; i++ )
        					{
        						indirect_data[i] = frame.data[i+1];//闂存帴鎸囦护瀛樺叆鏁扮粍杞彂UDP
        					}
        					if( count_jj_all < 255 )
            					count_jj_all ++;
    						else
    							count_jj_all = 1;
        					indirect_end = 66;//闂存帴鎸囦护鎺ユ敹瀹屾垚鏍囧織锛屽叏灞�鍙橀噺
        					DBG_MSG("CAN0_indirect------TESTING = %02x %02x \n", indirect_data[1], indirect_data[2]);//鎵撳嵃
        				}
        			}
        		}
        		/* 鍐呭姟鎸囦护*/
        		else if( (frame.can_id & 0x7ff) == aican_ID.action_id )
        		{
                    if( frame.can_dlc == 0x08 )
                    {
                    	if( (frame.data[0] == 0x58) && (frame.data[1] == 0xb2) && (frame.data[7] == action_recvsum(&frame)) )
                        {
                    		usleep(200);
                    		/*蹇冭烦娴嬭瘯锛屽彧闇�搴旂瓟*/
                    		if( frame.data[6] == 0xf1 )
                    		{
                    			senddata_action(fd_2, (struct sockaddr*)&addr_can0, addr_length, 0xf1);
                    			DBG_MSG("CAN0_heart------TESTING\n");
                    		}
                    		/*A绾垮浣�*/
                    		else if( frame.data[6] == 0xfa )
                    		{
                    			senddata_action(fd_2, (struct sockaddr*)&addr_can0, addr_length, 0xfa);
//								system("ip link set can0 down");
//								system("ip link set can0 up");
                    			DBG_MSG("CAN0_A_Reset------TESTING\n");
                    		}
                    		/*B绾垮浣�*/
                    		else if( frame.data[6] == 0xfb )
                    		{
                    			senddata_action(fd_2, (struct sockaddr*)&addr_can0, addr_length, 0xfb);
//								system("ip link set can1 down");
//								system("ip link set can1 up");
								DBG_MSG("CAN0_B_Reset------TESTING\n");
                    		}
            				if( count_nw < 255 )
            					count_nw ++;
    						else
    							count_nw = 1;
                    	}
                    }
        		}
        	}
        	/* 鎺ユ敹澶氬寘鏁版嵁  */
        	else if( (frame.can_id & 0x01) == CAN_MULTIPLE )
        	{
        		/* GNSS骞挎挱鏁版嵁  */
				if( ((frame.can_id & 0x7ff) == aican_ID.gnss_id) )
				{
					multiple_gnssdata(&frame, 0x81);
					printf("GPS -- GPS -- GPS !!!\n");
				}
				/* GNSS鏄熷巻骞挎挱鏁版嵁  */
				else if( ((frame.can_id & 0x7ff) == aican_ID.tle_id) )
				{
					multiple_tledata(&frame, 0x81);
				}
				/* 濮挎帶鏄熸晱骞挎挱鏁版嵁  */
//				else if( ((frame.can_id & 0x7ff) == aican_ID.starsensor_id) )
//				{
//					multiple_acsdata(&frame, 0x04);
//				}
				/* 濮挎帶杞ㄩ亾骞挎挱鏁版嵁  */
//				else if( ((frame.can_id & 0x7ff) == aican_ID.orbit_id) )
//				{
//					multiple_orbitdata(&frame, 0x04);
////					acs_txt = 66;/*************6666*********/
//					DBG_MSG("ACS datas = %d!\n",acs_txt);
//				}
        	}
          }
       }

    return NULL;
}




/* CAN3绾跨▼ */
void *thread_can_3(void *arg)
{
	/* CAN1鍒濆鍖栬繃绋嬪彉閲�  */
	    int ret, i, addr_length;
	    struct can_frame frame;

	    socklen_t socket_len = sizeof(struct sockaddr_can);
	    addr_length = sizeof(addr_can1);

		while( can_init_ready != 66 )
		{
		   usleep(100);
		}
		DBG_MSG("CAN INIT ready! \n");


	    while (1) {
	    	/* 浠巆an鎺ュ彛鎺ユ敹鏁版嵁  */
	        ret = recvfrom(fd_2, &frame, sizeof(struct can_frame), 0, (struct sockaddr *)&addr_can1, &socket_len);

	        /* 鍒ゆ柇鎺ユ敹鏁版嵁绫诲瀷涓鸿疆璇㈤仴娴嬨�佽疆璇㈠箍鎾�侀棿鎺ユ寚浠ゃ�佸唴鍔℃寚浠ゃ�丟NSS骞挎挱銆佸Э鎺ф槦鏁忓箍鎾紝鍏辫6绫绘暟鎹寘  */
	        if (ret > 0)
	          {
	        	/* 鎺ユ敹鍗曞寘鏁版嵁  */
	        	if( (frame.can_id & 0x01) == CAN_SINGLE )
	        	{
	        		/* 杞閬ユ祴銆侀仴娴嬪箍鎾�  */
	        		if( (frame.can_id & 0x7ff) == aican_ID.telemeter_id )
	        		{
	                    if( frame.can_dlc == 0x08 )
	                    {
	                    	if( (frame.data[0] == 0x48) && (frame.data[1] == 0xb2) && (frame.data[7] == telemeter_recvsum_1(&frame)) )
	                        {
	                    		usleep(200);
	                    		/* 杞閬ユ祴  */
	                    		if( frame.data[6] == CAN_YC_FDT )
	                    		{
	                    			senddata_normal_1(CAN_YC_FDT, fd_2, (struct sockaddr*)&addr_can1, addr_length);
	                    		}
	                    		/* 杞骞挎挱  */
	                    		else if( frame.data[6] == CAN_GB_FDT )
	                    		{
	                    			senddata_normal_1(CAN_GB_FDT, fd_2, (struct sockaddr*)&addr_can1, addr_length);
	                    			flag_orbit = 66;//绮惧瘑瀹氳建鏍囧織鍙橀噺锛�1s鏌ヨ涓�娆�
	                    			count_z7 ++;//z7绱绉掕鏁�
	                    		}
	                    	}
	                    	else
	                    		ERR_MSG("Error1 SUM: W7 = %02x  SUM = %02x ! \n", frame.data[7], telemeter_recvsum_1(&frame));
	                    }
	                    else
	                    	ERR_MSG("Error1 DLC: %d ! \n", frame.can_dlc);
	        		}
	        		/* 闂存帴鎸囦护  */
	        		else if( (frame.can_id & 0x7ff) == aican_ID.indirect_id )
	        		{
	        			if( frame.can_dlc == 0x06 )
	        			{
	        				if( (frame.data[0] == 0x20) && (frame.data[1] == 0xb2) && (frame.data[5] == indirect_recvsum(&frame)) )
	        				{
	        					usleep(200);
	        					senddata_unnormal(fd_2, (struct sockaddr*)&addr_can1, addr_length);//闂存帴鎸囦护搴旂瓟
	        					for( i=0; i<CAN_INDIRECT; i++ )
	        					{
	        						indirect_data[i] = frame.data[i+1];//闂存帴鎸囦护瀛樺叆鏁扮粍 杞彂UDP
	        					}
	        					if( count_jj_all < 255 )
	            					count_jj_all ++;
	    						else
	    							count_jj_all = 1;
	        					indirect_end = 66;//闂存帴鎸囦护鎺ユ敹瀹屾垚鏍囧織锛屽叏灞�鍙橀噺
	        					DBG_MSG("CAN1_indirect------TESTING = %02x %02x \n", indirect_data[1], indirect_data[2]);
	        				}
	        			}
	        		}
	        		/* 鍐呭姟鎸囦护 */
	        		else if( (frame.can_id & 0x7ff) == aican_ID.action_id )
	        		{
	                    if( frame.can_dlc == 0x08 )
	                    {
	                    	if( (frame.data[0] == 0x58) && (frame.data[1] == 0xb2) && (frame.data[7] == action_recvsum(&frame)) )
	                        {
	                    		usleep(200);
	                    		/* 蹇冭烦娴嬭瘯锛屽彧闇�搴旂瓟*/
	                    		if( frame.data[6] == 0xf1 )
	                    		{
	                    			senddata_action(fd_2, (struct sockaddr*)&addr_can1, addr_length, 0xf1);
	                    			DBG_MSG("CAN1_heart------TESTING\n");
	                    		}
	                    		/*A绾垮浣�*/
	                    		else if( frame.data[6] == 0xfa )
	                    		{
	                    			senddata_action(fd_2, (struct sockaddr*)&addr_can1, addr_length, 0xfa);
//									system("ip link set can0 down");
//									usleep(1000*100);
//									system("ip link set can0 up");
	                    			DBG_MSG("CAN1_A_Reset------TESTING\n");
	                    		}
	                    		/*B绾垮浣�*/
	                    		else if( frame.data[6] == 0xfb )
	                    		{
	                    			senddata_action(fd_2, (struct sockaddr*)&addr_can1, addr_length, 0xfb);
//									system("ip link set can1 down");
//									system("ip link set can1 up");
									DBG_MSG("CAN1_B_Reset------TESTING\n");
	                    		}
	            				if( count_nw < 255 )
	            					count_nw ++;
	    						else
	    							count_nw = 1;
	                    	}
	                    }
	        		}
	        	}
	        	/* 鎺ユ敹澶氬寘鏁版嵁  */
	        	else if( (frame.can_id & 0x01) == CAN_MULTIPLE )
	        	{
	        		/* GNSS骞挎挱鏁版嵁  */
					if( ((frame.can_id & 0x7ff) == aican_ID.gnss_id) )
					{
						multiple_gnssdata(&frame, 0x81);
					}
					/* GNSS鏄熷巻骞挎挱鏁版嵁  */
					else if( ((frame.can_id & 0x7ff) == aican_ID.tle_id) )
					{
						multiple_tledata(&frame, 0x81);
					}
					/* 濮挎帶鏄熸晱骞挎挱鏁版嵁  */
//					else if( ((frame.can_id & 0x7ff) == aican_ID.starsensor_id) )
//					{
//						multiple_acsdata(&frame, 0x04);
//					}
					/* 濮挎帶杞ㄩ亾骞挎挱鏁版嵁  */
//					else if( ((frame.can_id & 0x7ff) == aican_ID.orbit_id) )
//					{
//						multiple_orbitdata(&frame, 0x04);
////					    acs_txt = 66;/*************6666*********/
//					    DBG_MSG("ACS datas = %d!\n",acs_txt);
//					}
	        	}
	          }
	       }

	    return NULL;
}

















